{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- [Imputer](http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.Imputer.html#sklearn.preprocessing.Imputer)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from sklearn.preprocessing import Imputer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "X = [\n",
    "    [2, 2, 4, 1],\n",
    "    [np.nan, 3, 4, 4],\n",
    "    [1, 1, 1, np.nan],\n",
    "    [2, 2, np.nan, 3]\n",
    "]\n",
    "X2 = [\n",
    "    [2, 6, np.nan, 1],\n",
    "    [np.nan, 5, np.nan, 1],\n",
    "    [4, 1, np.nan, 5],\n",
    "    [np.nan, np.nan, np.nan, 1]\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Imputer(axis=1, copy=True, missing_values='NaN', strategy='mean', verbose=0)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 缺失值填充\n",
    "imp1 = Imputer(missing_values='NaN', strategy='mean', axis=0)\n",
    "imp2 = Imputer(missing_values='NaN', strategy='mean', axis=1) # 列填充？不是行\n",
    "imp1.fit(X)\n",
    "imp2.fit(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 2.          6.          3.          1.        ]\n",
      " [ 1.66666667  5.          3.          1.        ]\n",
      " [ 4.          1.          3.          5.        ]\n",
      " [ 1.66666667  2.          3.          1.        ]]\n",
      "--------------------------\n",
      "[[ 2.          6.          3.          1.        ]\n",
      " [ 3.          5.          3.          1.        ]\n",
      " [ 4.          1.          3.33333333  5.        ]\n",
      " [ 1.          1.          1.          1.        ]]\n"
     ]
    }
   ],
   "source": [
    "print (imp1.transform(X2))\n",
    "print (\"--------------------------\")\n",
    "print (imp2.transform(X2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Imputer(axis=0, copy=True, missing_values='NaN', strategy='most_frequent',\n",
       "    verbose=0)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "imp1 = Imputer(missing_values='NaN', strategy='mean', axis=0)\n",
    "imp2 = Imputer(missing_values='NaN', strategy='median', axis=0)\n",
    "imp3 = Imputer(missing_values='NaN', strategy='most_frequent', axis=0)\n",
    "imp1.fit(X)\n",
    "imp2.fit(X)\n",
    "imp3.fit(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2, 6, nan, 1], [nan, 5, nan, 1], [4, 1, nan, 5], [nan, nan, nan, 1]]\n",
      "--------------------------\n",
      "[[ 2.          6.          3.          1.        ]\n",
      " [ 1.66666667  5.          3.          1.        ]\n",
      " [ 4.          1.          3.          5.        ]\n",
      " [ 1.66666667  2.          3.          1.        ]]\n",
      "--------------------------\n",
      "[[ 2.  6.  4.  1.]\n",
      " [ 2.  5.  4.  1.]\n",
      " [ 4.  1.  4.  5.]\n",
      " [ 2.  2.  4.  1.]]\n",
      "--------------------------\n",
      "[[ 2.  6.  4.  1.]\n",
      " [ 2.  5.  4.  1.]\n",
      " [ 4.  1.  4.  5.]\n",
      " [ 2.  2.  4.  1.]]\n"
     ]
    }
   ],
   "source": [
    "print (X2)\n",
    "print (\"--------------------------\")\n",
    "print (imp1.transform(X2))\n",
    "print (\"--------------------------\")\n",
    "print (imp2.transform(X2))\n",
    "print (\"--------------------------\")\n",
    "print (imp3.transform(X2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from numpy import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.082978  , -0.22032449,  0.49988563,  0.19766743,  0.35324411,\n",
       "        0.40766141,  0.31373979,  0.15443927,  0.10323253, -0.03881673,\n",
       "        0.08080549, -0.1852195 ,  0.29554775, -0.37811744])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x=random.RandomState(1)\n",
    "0.5-x.rand(14)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
